package org.blogsomy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.blogsomy.model.dao.DaoFacade;
import org.blogsomy.model.entities.User;
import org.springframework.context.ApplicationContext;

import com.google.code.lightsomy.web.ApplicationContextRegistry;

public abstract class BaseController {

	public DaoFacade getDaoFacade() {
		return getBean("daoFacade");
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getBean(String bean) {
		ApplicationContext context = ApplicationContextRegistry.getApplicationContext();
		return (T) context.getBean(bean);
	}
	
	public User getUser(HttpServletRequest req) {
		HttpSession s = req.getSession();
		return s.getAttribute("user") == null ? null 
											  : (User) s.getAttribute("user");
	}
}
